package com.cl.edu.web.admin;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.cl.common.CrudActionSupport;
import com.cl.core.orm.Page;
import com.cl.core.web.struts2.Struts2Utils;
import com.cl.edu.entity.Bookingstudent;
import com.cl.edu.entity.Student;
import com.cl.edu.entity.Teacher;
import com.cl.edu.service.admin.BookingstudentManager;

/**
 * 教员主动预约学生信息管理
 * @author Administrator
 *
 */
public class BookingstudentAction extends CrudActionSupport<Bookingstudent>{

	@Autowired
	private BookingstudentManager bookingstudentManager;
	
	private Bookingstudent entity;	
	private Long id;
	
	private Page<Bookingstudent> page = new Page<Bookingstudent>();
	
	private String filter_realname;		//预约教员姓名
	private String filter_name;			//预约学员姓名
	private Integer state;				//预约状态
	
	/**
	 * 查询预约列表
	 */
	@Override
	public String list() throws Exception {
		if (!page.isOrderBySetted()) {
			page.setOrderBy("bookTime");
			page.setOrder(Page.DESC);
		}
		page = bookingstudentManager.getBookingstudents(page);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		long teacherId = Long.parseLong(Struts2Utils.getParameter("teacherId"));
		long studentId = Long.parseLong(Struts2Utils.getParameter("studentId"));
		Teacher teacher = bookingstudentManager.getTeacher(teacherId);
		Student student = bookingstudentManager.getStudent(studentId);
		entity.setTeacher(teacher);
		entity.setStudent(student);
		entity.setBookTime(new Date());
		entity.setState(1);//管理员加入的默认已审核
		bookingstudentManager.updateBooking(entity);//新增预约
		
		addActionMessage("新增预约成功，您可以到前台查看!");
		return list();
	}

	/**
	 * 删除预约信息
	 */
	@Override
	public String delete() throws Exception {
		bookingstudentManager.delBookingstudent(id);
		addActionMessage("删除预约信息成功!");
		return list();
	}
	
	/**
	 * 人工处理完预约信息后改变状态
	 * @return
	 * @throws Exception
	 */
	public String change() throws Exception {
		Bookingstudent bookingstudent = bookingstudentManager.getBookingstudent(id);
		bookingstudent.setState(1);
		bookingstudentManager.updateBooking(bookingstudent);
		addActionMessage("修改状态成功!");
		return list();
	}
	
	/**
	 * 查询通过审核的教员
	 * @return
	 * @throws Exception
	 */
	public String queryteacher() throws Exception {
		StringBuffer result = new StringBuffer();
		String q = Struts2Utils.getParameter("q");
		List<Teacher> list = bookingstudentManager.getTeachers(q);
		for(Teacher t:list){
			result.append(t.getId()).append("\n");
		}
		Struts2Utils.renderText(result.toString());
		return null;
	}
	
	/**
	 * 查询学员
	 * @return
	 * @throws Exception
	 */
	public String querystudent() throws Exception {
		StringBuffer result = new StringBuffer();
		String q = Struts2Utils.getParameter("q");
		List<Student> list = bookingstudentManager.getstudents(q);
		for(Student s:list){
			result.append(s.getId()).append("\n");
		}
		Struts2Utils.renderText(result.toString());
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		if(id == null){
			entity = new Bookingstudent();
		}else {
			entity = bookingstudentManager.getBookingstudent(id);
		}
	}
	
	public Bookingstudent getModel() {
		return entity;
	}

	public Bookingstudent getEntity() {
		return entity;
	}

	public void setEntity(Bookingstudent entity) {
		this.entity = entity;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}



	public String getFilter_realname() {
		return filter_realname;
	}

	public void setFilter_realname(String filter_realname) {
		this.filter_realname = filter_realname;
	}

	public String getFilter_name() {
		return filter_name;
	}

	public void setFilter_name(String filter_name) {
		this.filter_name = filter_name;
	}

	public Integer getState() {
		return state;
	}

	public void setState(Integer state) {
		this.state = state;
	}

	public Page<Bookingstudent> getPage() {
		return page;
	}

	public void setPage(Page<Bookingstudent> page) {
		this.page = page;
	}

	


}
